﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace Workshop
{
    public partial class FormAddinstructor : Form
    {
        public FormAddinstructor()
        {
            InitializeComponent();
        }

        private void FormAddinstructor_Load(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (rbInstructor.Checked)
            {
                XmlDocument xml = new XmlDocument();
                xml.Load("Staff.xml");         //load the Course.xml
                String path1 = "//Staffs/Instructors[@InstructorNumber]";     //find the attribute of course number
                XmlNode node1 = xml.SelectSingleNode(path1);
                int temp;
                int.TryParse(node1.Attributes[0].Value, out temp);      //change the type from string to integer
                temp++;         //add 1 course
                node1.Attributes[0].Value = temp.ToString();        //cover the old value
                xml.Save("Staff.xml");

                XmlDocument doc1 = new XmlDocument();
                doc1.Load("Staff.xml");
                XmlNode parent = doc1.SelectSingleNode("//Staffs/Instructors");
                XmlNode newnode = doc1.CreateElement("InstructorID");
                newnode.Attributes.Append(CreateAttribute(parent, "Name", txtName.Text));
                newnode.Attributes.Append(CreateAttribute(parent, "Workload", "0"));
                newnode.InnerText = temp.ToString();
                parent.AppendChild(newnode);
                doc1.Save("Staff.xml");

                MessageBox.Show("Add successfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //txtID.Text = "";
                txtName.Text = "";
            }
            else if (rbta.Checked)
            {
                XmlDocument xml = new XmlDocument();
                xml.Load("Staff.xml");         //load the Course.xml
                String path1 = "//Staffs/HeadTAs[@HeadTANumber]";     //find the attribute of course number
                XmlNode node1 = xml.SelectSingleNode(path1);
                int temp;
                int.TryParse(node1.Attributes[0].Value, out temp);      //change the type from string to integer
                temp++;         //add 1 course
                node1.Attributes[0].Value = temp.ToString();        //cover the old value
                xml.Save("Staff.xml");

                XmlDocument doc2 = new XmlDocument();
                doc2.Load("Staff.xml");
                XmlNode parent = doc2.SelectSingleNode("//Staffs/HeadTAs");
                XmlNode newnode = doc2.CreateElement("HeadTAID");
                newnode.Attributes.Append(CreateAttribute(parent, "Name", txtName.Text));
                newnode.Attributes.Append(CreateAttribute(parent, "Workload", "0"));
                newnode.InnerText = temp.ToString();
                parent.AppendChild(newnode);
                doc2.Save("Staff.xml");

                MessageBox.Show("Add successfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //txtID.Text = "";
                txtName.Text = "";
            }
            else MessageBox.Show("Please select one of the types!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        public XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value)
        {
            try
            {
                XmlDocument doc = node.OwnerDocument;
                XmlAttribute attr = null;
                attr = doc.CreateAttribute(attributeName);
                attr.Value = value;
                node.Attributes.SetNamedItem(attr);
                return attr;
            }
            catch (Exception err)
            {
                string desc = err.Message;
                return null;
            }
        }

        private void courseInformationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormCourseinformation form = new FormCourseinformation();
            form.Show();
            this.Hide();
        }

        private void courseTimeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormITgroups form = new FormITgroups();
            form.Show();
            this.Hide();
        }

        private void tAArrangementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1 form = new Form1();
            form.Show();
            this.Hide();
        }

        private void instructorInfomationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void dataGraphToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormInstructorInfoChart form1 = new FormInstructorInfoChart();
            this.Hide();
            form1.Show();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rbta_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FormHeadPixel headPixel = new FormHeadPixel();
            headPixel.Show();
            this.Close();
        }

        
    }
}
